/*
* Copyright 2010 ANDAGO INGENIERIA S.L.
*
* Licensed under the EUPL, Version 1.1 only (the "Licence");
* You may not use this work except in compliance with the
* Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in
* writing, software distributed under the Licence is
* distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
* See the Licence for the specific language governing
* permissions and limitations under the Licence.
*/

package com.andago.alohaui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;

public class SettingsActivity extends PreferenceActivity {
	
	private boolean both=false;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.settings);
		addPreferencesFromResource(R.xml.settings);
		
		Preference andagoUser = (Preference) findPreference("ausername");
		andagoUser.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
	
		@Override
		public boolean onPreferenceChange(Preference arg0, Object arg1) {
			// TODO Auto-generated method stub
			System.out.println("************ANDAGO USER PREFERENCE");
			Intent intent = new Intent(SettingsActivity.this,AlohaMainUI.class);		
		    startActivity(intent);
		    //SettingsActivity.this.finish();
			return true;
		}
		});
		
		Preference googleUser = (Preference) findPreference("gusername");
		googleUser.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference arg0, Object arg1) {
				// TODO Auto-generated method stub
				System.out.println("************GOOGLE USER PREFERENCE");
				
				SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
				String pass = settings.getString("gpassword", "");
				
				if (pass!="") both=true;
				
				if (both){
					Intent intent = new Intent(SettingsActivity.this,AlohaMainUI.class);
				    startActivity(intent);
				    //SettingsActivity.this.finish();
				}
				return true;
			}
			});
		
		Preference googlePassword = (Preference) findPreference("gpassword");
		googlePassword.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference arg0, Object arg1) {
				// TODO Auto-generated method stub
				System.out.println("************GOOGLE PASSWORD PREFERENCE");
				
				SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
				String user = settings.getString("gusername", "");
				
				if (user!="") both=true;
				
				if (both){
					Intent intent = new Intent(SettingsActivity.this,AlohaMainUI.class);
				    startActivity(intent);
				    //SettingsActivity.this.finish();
				}
				return true;
			}
			});
	}
	
	@Override
	public void onStop(){
		super.onStop();
		this.finish();
	}
	@Override
	public void onDestroy(){
		super.onDestroy();
	}
}